package it.carlom.stickyheader.example.fragment;

import it.carlom.stickyheader.ResourceTable;
import it.carlom.stickyheader.example.Utils;
import it.carlom.stickyheader.example.adapter.TextHideLineAdapter;
import it.carlom.stikkyheader.core.StikkyHeaderBuilder;
import it.carlom.stikkyheader.core.utils.PxUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

import java.util.List;

public class RecyclerStikkyFragment extends BaseFraction {

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        return scatter.parse(ResourceTable.Layout_fragment_simplelistview, container, false);
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        ListContainer listView = (ListContainer) getComponent().findComponentById(ResourceTable.Id_listview);
        Component header = getComponent().findComponentById(ResourceTable.Id_header);

        List<String> data = Utils.getData();
        TextHideLineAdapter itemProvider = new TextHideLineAdapter(getFractionAbility(), data);
        listView.setItemProvider(itemProvider);
        listView.setItemClickedListener((listContainer, component, position, l) -> {
            String str = data.get(position);
            if (!data.get(position).contains("+")) {
                str = str + " +";
            }
            data.add(position, str);
            itemProvider.notifyDataSetItemInserted(position);
        });
        listView.setItemLongClickedListener((listContainer, component, i, l) -> {
            data.remove(i);
            itemProvider.notifyDataSetItemRemoved(i);
            return false;
        });

        StikkyHeaderBuilder.stickTo(listView)
                .setHeader(header)
                .minHeightHeader((int) PxUtil.vp2px(120))
                .build();
    }

}
